﻿using System.Windows;

namespace TestSocket
{    
    public enum ePanel
    {
        pnlStart, pnlConfig, pnlConnectedSockets, pnlAcceptedSocket
    }

    public interface IPanel
    {
        void OnActivate();
        void OnDeactivate();
        void OnAppClosing();
    }
    public partial class MainWindow : Window
    {        
        public string strDestIP{get;set;}
        public string strSrcIP { get; set; }
        public string strMsgPort{get; set;}
        public string strBinaryPort{get; set;}
        public bool bServerMode=false;        
                        
        IPanel m_curPanel = null;
        IPanel m_pnlStart = null;
        IPanel m_pnlConfig = null;
        IPanel m_pnlConnectedSockets = null;
        IPanel m_pnlAcceptedSockets = null;

        public ePanel nxtScrn
        {
            set
            {
                ChangePanel(value);
            }
        }
        public MainWindow()
        {
            InitializeComponent();                                    
            ChangePanel(ePanel.pnlStart);
        }

        void ChangePanel(ePanel pnl)
        {
            IPanel nxtPanel = null;
            WpfPageTransitions.PageTransitionType type = WpfPageTransitions.PageTransitionType.SlideAndFade;
            if (m_curPanel != null)            
                m_curPanel.OnDeactivate();                
            switch(pnl)
            {
                case ePanel.pnlConfig:
                    {
                        if (m_pnlConfig == null)
                            m_pnlConfig = new ConfigPanel(this);
                        nxtPanel = m_pnlConfig;
                        //type = WpfPageTransitions.PageTransitionType.GrowAndFade;
                    }
                    break;
                case ePanel.pnlConnectedSockets:
                    {
                        if (m_pnlConnectedSockets == null)
                            m_pnlConnectedSockets = new ConnectedSocketsPanel(this);
                        ////type = WpfPageTransitions.PageTransitionType.FlipAndFade;
                        nxtPanel = m_pnlConnectedSockets;
                    }
                    break;
                case ePanel.pnlStart:
                    {
                        if (m_pnlStart == null)
                            m_pnlStart = new Choice(this);
                        //type = WpfPageTransitions.PageTransitionType.Fade;
                        nxtPanel = m_pnlStart;
                    }
                    break;
                case ePanel.pnlAcceptedSocket:
                    {
                        if (m_pnlAcceptedSockets == null)
                            m_pnlAcceptedSockets = new AcceptedSockets(this);
                        //type = WpfPageTransitions.PageTransitionType.SpinAndFade;
                        nxtPanel = m_pnlAcceptedSockets;
                    }
                    break;
            }
            if (nxtPanel!=null)
            {
                m_curPanel = nxtPanel;                
                pageTransitionControl.TransitionType = type;
                pageTransitionControl.ShowPage((System.Windows.Controls.UserControl)m_curPanel);
                m_curPanel.OnActivate();
            }
        }
                     
        public void dprintf(string s)
        {
            if (!Dispatcher.CheckAccess())
                Dispatcher.Invoke(new System.Action(()=>DisplayMsg(s)));
            else
                DisplayMsg(s);
        }
        
        void DisplayMsg(string s)
        {
            lbMsg.Items.Add(s);
        }

        private void OnClear_Click(object sender, RoutedEventArgs e)
        {
            lbMsg.Items.Clear();
        }

        private void Window_Closed(object sender, System.EventArgs e)
        {
            m_curPanel.OnAppClosing();
        }

        
    }    
}
